#include"head.h"
int client_login(int socket_fd,user *user)
{
    //输入用户名和密码，进行登录验证
    user->flag = 1;
    printf("please input your username and password\n");
    printf("username:");
    scanf("%s",user->user_name);
    printf("password:");
    scanf("%s",user->password);

    //将读取到的用户信息传递给服务器
    send(socket_fd,user,sizeof(struct user_s),MSG_NOSIGNAL);

    int flag = -1;
    recv(socket_fd,&flag,sizeof(flag),MSG_WAITALL);
    if(flag == 0){
        printf("登录成功，欢迎您：%s\n",user->user_name);
    }
    if(flag == 1){
        close(socket_fd);
        printf("用户名不存在！\n");
    }
    if(flag == 2){
        close(socket_fd);
        printf("密码错误！\n");
    }

    return flag;   
}

int client_account_create(int socket_fd,user*user)
{
    //输入用户名和密码，进行登录验证
    user->flag = 2;
    printf("please input your username and password\n");
    printf("username:");
    scanf("%s",user->user_name);
    printf("password:");
    scanf("%s",user->password);

    printf("%s %s\n",user->user_name,user->password);
    //将读取到的用户信息传递给服务器
    send(socket_fd,user,sizeof(struct user_s),MSG_NOSIGNAL);

    int flag = -1;
    recv(socket_fd,&flag,sizeof(flag),MSG_WAITALL);
    if(flag == 0){
        printf("创建成功，欢迎您：%s\n",user->user_name);
        close(socket_fd);
    }else{
        printf("创建失败，用户名重复或密码不合法\n");
        close(socket_fd);
    }
    return flag;
}


int function_select(int * socket_fd, char *port, char *ip,struct user_s *user){
    printf("欢迎使用，请选择功能：\n");

    while(1){
        char func_code[255]={0};
        printf("1: 登录账号\t2: 注册账号\t3: 退出程序\n");
        //安全录入字符串
        
        scanf("%254s",func_code);

        printf("func_code: %s\n",func_code);
        if(strcmp(func_code,"3")==0){
            printf("感谢使用！\n");
            return -1;
        }

        socket_init(socket_fd,port,ip);
        memset(user,0,sizeof(struct user_s));
        if(strcmp(func_code,"1")==0){
            int ret = client_login(*socket_fd,user);
            if(ret == 0){
                break;
            }else continue;
        }
            if(strcmp(func_code,"2")==0){
                client_account_create(*socket_fd,user);
                continue;
            }
            printf("请输入正确的指令\n");
    }
    return 0;
}
